'use strict';

console.log(GetUrlParam('token'), GetUrlParam('user_id'), '活动id', GetUrlParam('activity_id'), window.location, '---------------');
vueObj = new Vue({
    el: '#page',
    data: function data() {
        return {region:'',
        total_golds:'',
            'A':'A :',
            'B':'B :',
            'C':'C :',
            'D':'D :',
            'E':'E :',
            'F':'F :',
            'G':'G :',
             'H':'H :',
             'I':'I :',
             'J':'J :',
             'K':'K :',
             'L':'L :',
         imgStr: '',
         phoneLanguage:GetUrlParam('language'),
         imgUrl:[
                
         ],
         ceshi:[[
         {gift_id: "1",
         gift_name: "Rose",
         gift_num: "77",
         gift_type: "6",
         gold_price: "555",
         level: "6",
         plan_id: "6001",
         selfPrice: 42735}
         ],
         [
            {gift_id: "1",
            gift_name: "Rose",
            gift_num: "77",
            gift_type: "6",
            gold_price: "555",
            level: "6",
            plan_id: "6001",
            selfPrice: 42735},{gift_id: "1",
            gift_name: "Rose",
            gift_num: "77",
            gift_type: "6",
            gold_price: "555",
            level: "6",
            plan_id: "6001",
            selfPrice: 42735}
            ],
            [
                {gift_id: "1",
                gift_name: "Rose",
                gift_num: "77",
                gift_type: "6",
                gold_price: "555",
                level: "6",
                plan_id: "6001",
                selfPrice: 42735},
                {gift_id: "1",
            gift_name: "Rose",
            gift_num: "77",
            gift_type: "6",
            gold_price: "555",
            level: "6",
            plan_id: "6001",
            selfPrice: 42735}
                ]],
         videStr:'',
         typeab:0,
         dFlag:false,
         typeIv:2,
            demo:[
                [
                    {gift_name:'Plan A', gift_num:1}
                ],
                [
                    {gift_name:' Plan B', gift_num:2}
                ],                    [
                    {gift_name:'Plan C', gift_num:3}
                ],                    [
                    {gift_name:'Plan D', gift_num:4}
                ],          
            ],
            type:[
            
            ],
            flag:false,
            tabT:1,
            isRule: false,
            lan: {},
            lanType:'',
            region:'',
            list: [['A', 10,80000, [8000]],
             ['B', 15, 125000,[10000]], 
             ['C', 15, 250000,[20000]], 
              ['D', 20,375000, [30000]], 
              ['E', 25,625000, [50000]], 
              ['F', 25, 750000,[60000]], 
               ['G', 30,875000, [70000]], 
               ['H', 35,1000000, [80000]], 
               ['I', 40,1250000, [100000]], 
               ['J', 40,1750000, [140000]], 
               ['K', 40,2500000, [200000]], 
               ['L', 40,3750000, [300000]], 
             

               
             ],
            user_list: [{
                img: 'https://oss.chathot.me/action/2021/07/20/img1.png',
                no: '10080',
                id: '100001',
                t: 'admin_80'
            }, {
                img: 'https://oss.chathot.me/action/2021/07/20/img2.png',
                no: '10016',
                id: '100127',
                t: ' Aimee'
            }, {
                img: 'https://oss.chathot.me/action/2021/07/20/img1.png',
                no: '10086',
                id: '100011',
                t: ' admin_86'
            }],
            roomlist: [],
            showMsg: {},
            currentLan: 'en',
            // 活动扶持开始
            supportMark: false,
            rewardSelect: 0, // 奖励选择
            videoPath: '',
            form: {
                file: ''
            },
            activeId: GetUrlParam('activity_id') || '',
            appActiceId: GetUrlParam('activity_id') || '',
            // appActiceId:'718',
            activeData: {
                con_list: [],
                plan_info: []
            },
            enarRuleMark: false,
            trHiIdRuleMark: false,
            videoUploadUrl: '',
            user_id: GetUrlParam('user_id') || '',
            // 活动扶持开始结束
        };
    },
    watch:{
        imgStr(newUrl){
            console.log(newUrl,"连接");
         if( newUrl.indexOf('mp4')>-1){
             this.videStr=newUrl
             console.log(this.videStr);
             return
         }
        if (this.imgUrl.length > 2) return
          this.imgUrl.push(newUrl)
          console.log('image',this.imgUrl);
        },
//         videStr(newUrl){
// console.log(newUrl)
// this.videoUrl=newUrl
// console.log(this.videoUrl);
//         }
    },
    created: function created() {
        this.setLanguage(1)
        //     try {
        //             if (isIphone()) {
        //                 window.webkit.messageHandlers.selectMedia.postMessage
        //                 } else {
        //                 // media_type 需要传1或者2，传10不会调起app方法
        //                 window.WebViewJavascriptBridge.callHandler(
        //                     'selectMedia'
        //                     , {'media_type': 10}
        //                     , function(responseData) {
        //                         
        //                     }
        //                 );
        //             }
        //     this.dFlag=true
        //             console.log('新版支持视频上传的')
        //         } catch(err) 
        //           console.log(err);
        //   this.dFlag=false
        //           console.log('旧版支持视频上传的')
        //         }
        console.log(GetUrlParam('token'), GetUrlParam('user_id'), '---------------');
        // this.lan = judge_lan(GetUrlParam('language'), language);


        // 活动扶持开始
        if (this.activeId) {
            this.getData();
        }
        this.extData = {
            supportLevel: {
                arEn: [{
                    type: 'A',
                    num: 10,
                    gold: 80000
                }, {
                    type: 'B',
                    num: 15,
                    gold: 125000
                }, {
                    type: 'C',
                    num: 15,
                    gold: 250000
                }, {
                    type: 'D',
                    num: 20,
                    gold: 375000
                }, {
                    type: 'E',
                    num: 25,
                    gold: 625000
                }, {
                    type: 'F',
                    num: 25,
                    gold: 750000
                }, {
                    type: 'G',
                    num: 30,
                    gold: 875000
                }, {
                    type: 'H',
                    num: 35,
                    gold: 1000000
                }, {
                    type: 'I',
                    num: 40,
                    gold: 1250000
                }, {
                    type: 'J',
                    num: 40,
                    gold: 1750000
                }, {
                    type: 'K',
                    num: 40,
                    gold: 2500000
                }, {
                    type: 'L',
                    num: 40,
                    gold: 3750000
                }],
                trHiId: [{
                    type: 'A',
                    num: 10,
                    gold: 50000
                }, {
                    type: 'B',
                    num: 15,
                    gold: 100000
                }, {
                    type: 'C',
                    num: 20,
                    gold: 200000
                }, {
                    type: 'D',
                    num: 25,
                    gold: 300000
                }, {
                    type: 'E',
                    num: 25,
                    gold: 400000
                }, {
                    type: 'F',
                    num: 30,
                    gold: 500000
                }]
            }
        };
        // 活动扶持开始结束        
    },
    methods: {
        setLanguage(region) {     
            if (region !== '') {
                switch (parseInt(region)) {
                    case 0:
                    case 1:
                        if (this.phoneLanguage == 'ar') {
                            this.lanType = 'ar'
                            // console.log('语言类型a,', this.lanType)
                        }else if(this.phoneLanguage == 'fa'){
//  console.log('语言类型a,', this.lanType)
                            this.lanType = 'fa'
                        }
                        else {
                            this.lanType = 'en'
                            // console.log('语言类型b,', this.lanType)
                        }
                        break;
                    case 3:
                        this.lanType = 'tr'
                        // console.log('语言类型c,', this.lanType)
                        break;
                    case 4:
                        this.lanType = 'hi'
                        // console.log('语言类型d,', this.lanType)
                        break;
                    case 10:
                        this.lanType = 'id'
                        // console.log('语言类型e,', this.lanType)
                        break;
                    case 9:
                        this.lanType = 'es'
                        // console.log('语言类型es,', this.lanType)
                        break;
                    default:
                        this.lanType = 'hi'
                    // console.log('语言类型默认hi,', this.lanType)

                }
            } else {
                this.lanType = 'hi'
                // console.log('语言类型都没有就hi,', this.lanType)
            }
            document.body.classList.add(this.lanType)
            console.log('```', this.lanType);
            if (this.lanType== 'ar') {
         document.getElementsByTagName('body')[0].className = 'ar_text';
     }
     if (this.lanType == 'fa') {
         document.getElementsByTagName('body')[0].className = 'ar_text';
     }
            this.lan = language[this.lanType]
            //选择A b
            this.type=[
                { typeA:this.lan.a42},
                {typeA:this.lan.a43}
            ]
            //规则
            this.roomlist = [{
                avatar: 'https://oss.chathot.me/action/2021/08/13/t13.png',
                b: 'https://oss.chathot.me/action/2021/08/13/t12.png',
                tit: this.lan.a28,
                x: [this.lan.k6, this.lan.k6_1, this.lan.k6_2, this.lan.k6_3, this.lan.k6_4, this.lan.k6_5, this.lan.k6_6],
                t: this.lan.k12,
                r: this.lan.k18
            }, {
                avatar: 'https://oss.chathot.me/action/2021/08/13/t11.png',
                b: 'https://oss.chathot.me/action/2021/08/13/t10.png',
    
                tit: this.lan.a29,
                x: [this.lan.k7, this.lan.k7_1, this.lan.k7_2],
                t: this.lan.k13,
                r: this.lan.k19
            }, {
                avatar: 'https://oss.chathot.me/action/2021/08/13/t7.png',
                b: 'https://oss.chathot.me/action/2021/08/13/t6.png',
    
                tit: this.lan.a30,
                x: [this.lan.k8],
                t: this.lan.k14,
                r: this.lan.k20
            }, {
                avatar: 'https://oss.chathot.me/action/2021/08/13/t4.png',
                b: 'https://oss.chathot.me/action/2021/08/13/t3.png',
    
                tit:this.lan.a31,
                x: [this.lan.k9, this.lan.k9_1],
                t: this.lan.k15,
                r: this.lan.k21
            }, {
                avatar: 'https://oss.chathot.me/action/2021/08/13/t9.png',
                b: 'https://oss.chathot.me/action/2021/08/13/t8.png',
    
                tit: this.lan.a32,
                x: [this.lan.k10],
                t: this.lan.k16,
                r: this.lan.k22
            }, {
                avatar: 'https://oss.chathot.me/action/2021/08/13/t2.png',
                b: 'https://oss.chathot.me/action/2021/08/13/t1.png',
    
                tit: this.lan.a33,
                x: [this.lan.k11],
                t: this.lan.k17,
                r: this.lan.k23
            }];
    
            console.log( this.lan);
            document.title = this.lan.a51;
        },
        changeVideStr(){
            this.videStr=''
        },
        
        setTxt: function setTxt(msg) {
            this.showMsg = msg;
            this.isRule = true;
        },
        // 跳转个人主页
        go_user: function go_user(id) {
            userDetails(id);
        },
        //tab切换
       changeTab(num){
        this.tabT=num
         },
        closeImage(index){
        this.imgUrl=this.imgUrl.filter((item, i) => i !== index)
        },
        // 房间跳转
        roomDetails: function (_roomDetails) {
            function roomDetails(_x) {
                return _roomDetails.apply(this, arguments);
            }

            roomDetails.toString = function () {
                return _roomDetails.toString();
            };

            return roomDetails;
        }(function (item) {
            roomDetails('451334', '', 'chinese451334');
        }),
        // 活动扶持开始
        supportMarkClick: function supportMarkClick(e) {
            if (e.target.id == 'support-content') {
                this.supportMark = false;
            }
        },                //上传
                selectMedia(media_type){
                      if (isIphone()) {
                        window.webkit.messageHandlers.selectMedia.postMessage({media_type: media_type});
                      } else {
                        window.WebViewJavascriptBridge.callHandler(
                          'selectMedia'
                          , {'media_type': media_type}
                          , function(responseData) {
                    
                          }
                        );
                      }
                    },
        fileChange: function fileChange(e) {
            var files = e.target.files;
            var filesItem = files[0];
            if (!filesItem) return;
            var maxSize = 2 * 1024 * 1024;
            console.log(files);
            if (filesItem.size > maxSize) {
                this.$refs.uploadIpt.value = '';
                toast(this.lan.k56);
                return;
            }
            if (filesItem.type.indexOf('image') === -1) {
                toast(this.lan.k57);
                this.$refs.uploadIpt.value = '';
                return;
            }
            console.log(filesItem.size, '打印size123');
            console.log('打印size123');
            this.form.file = filesItem;
            var objUrl = this.getObjectURL(filesItem);
            if (objUrl) {
                console.log(objUrl);
                this.videoPath = objUrl;
            }
        },
        //建立一个可存取到该file的url
        getObjectURL: function getObjectURL(file) {
            var url = null;
            if (window.createObjectURL != undefined) {
                // basic
                url = window.createObjectURL(file);
            } else if (window.URL != undefined) {
                // mozilla(firefox)
                url = window.URL.createObjectURL(file);
            } else if (window.webkitURL != undefined) {
                // webkit or chrome
                url = window.webkitURL.createObjectURL(file);
            }
            return url;
        },
        removeObjectURL: function removeObjectURL(ObjectFile) {
            var url = null;
            if (window.revokeObjectURL != undefined) {
                // basic
                url = window.revokeObjectURL(ObjectFile);
            } else if (window.URL != undefined) {
                // mozilla(firefox)
                url = window.URL.revokeObjectURL(ObjectFile);
            } else if (window.webkitURL != undefined) {
                // webkit or chrome
                url = window.webkitURL.revokeObjectURL(ObjectFile);
            }
            return url;
        },
        deleteVideo: function deleteVideo() {
            this.videoPath = '';
            this.videoUploadUrl = '';
            this.form.file = '';
            if (this.$refs.uploadIpt) {
                this.$refs.uploadIpt.value = '';
            }
            this.removeObjectURL(this.videoPath);
        },
        uploadVideo: function uploadVideo() {
            var _this = this;

            if (!this.form.file) {
                toast(this.lan.k53);
                return;
            }
            createLoad();
            var query = {
                user_token: GetUrlParam('token'),
                user_id: GetUrlParam('user_id'),
                file: this.form.file
            };
            return ajax2("POST", query, {
                hideLoading: false,
                formDataFlag: true
            }, '/api/Video/upVideo').then(function (res) {
                removeLoad();
                console.log(res);
                res.data = res.data || {};
                _this.videoUploadUrl = res.data.url;
                return res.data;
            }).catch(function (err) {
                removeLoad();
            });
        },
        // submitClick: function submitClick() {
        //     var _this2 = this;

        //     if (this.videoUploadUrl) {
        //         this.handleUpload();
        //     } else {
        //         var uploadThen = this.uploadVideo();
        //         if (!uploadThen) return;
        //         uploadThen.then(function (res) {
        //             _this2.handleUpload();
        //         });
        //     }
        // },
        submitChange: function submitClick() {
            console.log('223');
            var _this3 = this;
if(this.videStr=='' && this.imgUrl.length==0){
    return toast(_this3.lan.a41)
} if (this.typeab=='1') {
    if(this.imgUrl.length!=3){
        return toast(_this3.lan.a52)
    }
 }
            // if (!this.form.file) {
            //     return toast(this.lan.k57);
            // }
            var selectItem = this.activeData.plan_info[this.rewardSelect];
            console.log(selectItem, this.rewardSelect, this.activeData.plan_info);
            if (!selectItem || !selectItem[0] || !selectItem[0].plan_id) {
                return;
            }
            createLoad();
            var sureData = {
                "Api_name": 'activity_support',
                "Api_data": JSON.stringify({
                    // "video_url": this.videoUploadUrl,
                    "token": GetUrlParam('token'),
                    "op": "room_sup_apply",
                    "activity_id": this.activeId,
                    "plan_id": selectItem[0].plan_id,
                    "user_id": GetUrlParam('user_id'),
                    "type":this.typeIv,
                    "video_url": this.videStr==''? this.imgUrl.join("|"):this.videStr
                   
                }),
                // 'thumb': this.form.file,
            };
            // console.log(sureData);
            return ajax('POST', API.api_request, sureData, { formDataFlag: true, }).then(function (res) {
                removeLoad();
                if (res.status == 200) {
                    toast(_this3.lan.k49);
                    _this3.supportMark = false;
                    _this3.getData();
                } else {
                    toast(_this3.lan.a50);
                }
            }).catch(function (err) {});
        }, 
 
        submitClick: function submitClick() {
            
            var _this3 = this;

            if (!this.form.file) {
                return toast(this.lan.k57);
            }
            var selectItem = this.activeData.plan_info[this.rewardSelect];
            console.log(selectItem, this.rewardSelect, this.activeData.plan_info);
            if (!selectItem || !selectItem[0] || !selectItem[0].plan_id) {
                return;
            }
            createLoad();
            var sureData = {
                "Api_name": 'activity_support',
                "Api_data": JSON.stringify({
                    // "video_url": this.videoUploadUrl,
                    "token": GetUrlParam('token'),
                    "op": "room_sup_apply",
                    "activity_id": this.activeId,
                    "plan_id": selectItem[0].plan_id,
                    "user_id": GetUrlParam('user_id'),
                }),
                'thumb': this.form.file,
            };
            // console.log(sureData);
            return ajax('POST', API.api_request, sureData, { formDataFlag: true, }).then(function (res) {
                removeLoad();
                if (res.status == 200) {
                    toast(_this3.lan.k49);
                    _this3.supportMark = false;
                    _this3.getData();
                } else {
                    toast(lan.a50);
                }
            }).catch(function (err) {});
        },        
        handleUpload: function handleUpload() {
            var _this3 = this;

            if (!this.videoUploadUrl) {
                return toast(this.lan.k53);
            }
            var selectItem = this.activeData.plan_info[this.rewardSelect];
            console.log(selectItem, this.rewardSelect, this.activeData.plan_info);
            if (!selectItem || !selectItem[0] || !selectItem[0].plan_id) {
                return;
            }
            createLoad();
            var sureData = {
                "Api_name": 'activity_support',
                "Api_data": JSON.stringify({
                    "video_url": this.videoUploadUrl,
                    "token": GetUrlParam('token'),
                    "op": "room_sup_apply",
                    "activity_id": this.activeId,
                    "plan_id": selectItem[0].plan_id,
                    "user_id": GetUrlParam('user_id')
                })
            };
            console.log(sureData);
            return ajax('POST', API.api_request, sureData).then(function (res) {
                removeLoad();
                if (res.status == 200) {
                    toast(_this3.lan.k49);
                    _this3.supportMark = false;
                    _this3.getData();
                } else {
                    toast('Fail, ' + res.info + res.status);
                }
            }).catch(function (err) {});
        },

        getData: function getData() {
            var _this4 = this;

            createLoad();
            var sureData = {
                "Api_name": 'activity_support',
                "Api_data": JSON.stringify({
                    "token": GetUrlParam('token'),
                    "op": "get_room_active_info",
                    "activity_id": this.activeId,
                    "user_id": GetUrlParam('user_id')
                })
            };
            return ajax('POST', API.api_request, sureData).then(function (res) {
                removeLoad();
                res.data = res.data || {};
                res.data.con_list = res.data.con_list || [];
                res.data.plan_info = res.data.plan_info || [];
                console.log(res.data)
                // console.log(res.data.region);
            //   this.region=res.data.region
            //   console.log(this.region);
            _this4.total_golds=res.data.total_gold
            // _this4.setLanguage(res.data.region)
                for (var i = 0; i < res.data.plan_info.length; i++) {
                    var selfPrice = 0;
                    var planItem = res.data.plan_info[i];
                    for (var j = 0; j < planItem.length; j++) {
                        selfPrice = selfPrice * 1 + (res.data.plan_info[i][j].gold_price * 1 * (res.data.plan_info[i][j].gift_num * 1 || 0));
                    }
                    res.data.plan_info[i][0].selfPrice = selfPrice;
                }
                _this4.activeData = res.data;

                var typeStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                var resultType = [];

                var levelType = _this4.handleLevelType();
                if (!levelType) {
                    _this4.activeData.plan_info = [];
                } else {
                    for (var _i = 0; _i < res.data.plan_info.length; _i++) {
                        var _planItem = res.data.plan_info[_i];
                        if (_planItem[0] && _planItem[0].level == typeStr.indexOf(levelType) + 1) {
                            resultType.push(_planItem);
                        }
                    }
                    _this4.activeData.plan_info = resultType;
                }

                if (res.status == 200) {};
            }).catch(function (err) {
                console.log(err);
                removeLoad();
            }).catch(function (err) {
                console.log(err);
            });
        },
        supportBtnClick: function supportBtnClick() {
            // this.supportMark = true;
            // 已经申请了这个了
            if (this.activeData.apply_status == 1 || this.activeData.apply_status == 3 || this.activeData.apply_status == 4) return;
            // 是否在活动结束后的七天内
            var endTime = this.activeData.end_time;
            var nowTime = this.activeData.now_time;
            // 超过七天 或 活动还没结束
            if (nowTime - endTime >= 3600 * 24 * 7 || nowTime - endTime < 0) {
                toast(this.lan.k40);
                return;
            }
            // 当前房间 该房间有待发扶持 即已经申请过了还在审批，然后又申请了一次
            if (this.activeData.room_status == 1) {
                toast(this.lan.k41);
                return;
            }
            // 此房间当前是否有其他活动正在申请扶持
            if (this.activeData.week_count > 2 && (this.activeData.apply_status == 1 || this.activeData.apply_status == 3)) {
                toast(this.lan.k41);
                return;
            }
            // 每个区的固定规则
            if (this.currentLan === 'tr' || this.currentLan === 'id' || this.currentLan === 'hi') {
                if (!(this.activeData.week_count < 2 && this.activeData.count >= 10  && this.activeData.total_gold >= 50000)) {
                    // 弹窗
                    this.trHiIdRuleMark = true;
                    return;
                }
            } else {
                if (!(this.activeData.week_count < 2 && this.activeData.count >= 10  && this.activeData.total_gold >= 80000)) {
                    // 弹窗
                    this.enarRuleMark = true;
                    return;
                }
            }
            this.supportMark = true;
        },
        handleLevelType: function handleLevelType() {
            var count = this.activeData.count;
            var total_gold = this.activeData.total_gold;
            // console.log(count, total_gold);
            var awardArr = [];
            if (this.currentLan === 'tr' || this.currentLan === 'id' || this.currentLan === 'hi') {
                awardArr = this.extData.supportLevel.trHiId;
            } else {
                awardArr = this.extData.supportLevel.arEn;
            }
            for (var i = awardArr.length - 1; i >= 0; i--) {
                var item = awardArr[i];
                if (count >= item.num && total_gold >= item.gold) {
                    return item.type;
                }
            }
            return '';
        },
        rewardItemClick: function rewardItemClick(index) {
            this.rewardSelect = index;
        },
        typeClick(index){
            this.typeab=index
            if(this.typeab==0){
                this.imgUrl=[]
                this.typeIv=2
            }else{
                this.videStr=''
                this.typeIv=1
              
            }
          

    //  console.log(this.imgUrl.length);
    //  console.log(this.imgUrl.length<=2);
        }
        // 活动扶持开始结束            
    },
    mounted: function mounted() {
       
        var height = document.documentElement.clientHeight || document.body.clientHeight;
        var page = document.querySelector('#page');
        page.style.minHeight = height + 'px';
    }

});
// vueObj = new Vue({
//     el: '#starchat-applz',
//     render: h => h(app),
//   })